package com.saileijieji.mymvp.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.saileijieji.mylibrary.SharedPreferenceUtil;
import com.saileijieji.mymvp.R;
import com.saileijieji.mymvp.bean.LoginBean;
import com.saileijieji.mymvp.mvp.MvpdemoActivity;
import com.saileijieji.mymvp.mvp.base.BaseActivity;
import com.saileijieji.mymvp.view.main.MainNavigationpageActivity;
import com.saileijieji.mymvp.view.main.PollingUploadingActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @describe: 登录
 * @author: 武梁
 * @date: 2018/8/6 10:49
 * @mailbox: 1034905058@qq.com
 */
public class LoginActivity extends BaseActivity {

    @Bind(R.id.login_lay_loge)
    LinearLayout loginLayLoge;
    @Bind(R.id.login_edit_user)
    EditText loginEditUser;
    @Bind(R.id.login_edit_password)
    EditText loginEditPassword;
    @Bind(R.id.login_check)
    CheckBox loginCheck;
    @Bind(R.id.login_lay_btn)
    LinearLayout loginLayBtn;
    private long mExitTime = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @Override
    public void showData(int tag, Object data) {
        if (tag==0) {
            LoginBean bean=(LoginBean)data;
            toast("登录成功");
            startActivity(new Intent(this,MainNavigationpageActivity.class));
        }

    }

    @OnClick(R.id.login_lay_btn)
        public void onViewClicked() {
            startActivity(new Intent(this,PollingUploadingActivity.class));
        if (TextUtils.isEmpty(loginEditUser.getText())) {
            toast("请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(loginEditPassword.getText())) {
            toast("请输入密码");
            return;
        }
        presenter.getData(0, LoginActivity.this, false, apiManager.getLogin(loginEditUser.getText().toString(),loginEditPassword.getText().toString()));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        //1.点击的时间差如果大于2000，则提示用户点击两次退出
        if (System.currentTimeMillis() - mExitTime > 2000) {
            //2.保存当前时间
            mExitTime = System.currentTimeMillis();
            //3.提示
            toast("再按一次后退键退出程序");
        } else {
            //4.点击的时间差小于2000，调用父类onBackPressed方法执行退出。
            super.onBackPressed();
        }
    }
}
